# -*- encoding: utf-8 -*-


class Node(object):
    def __init__(self, data):
        self.data = data
        self.next = None


class LinkedList(object):
    def __init__(self):
        self.Head = None

    def insert_head(self, data):
        new_node = Node(data)
        if self.Head is not None:
            new_node.next = self.Head
        self.Head = new_node

    def insert_tail(self, data):
        if self.Head is None:
            self.insert_head(data)
        else:
            temp = self.Head
            while temp.next is not None:
                temp = temp.next
            temp.next = Node(data)

    def print_list(self):
        temp = self.Head
        while temp is not None:
            print(temp.data)
            temp = temp.next

    def delete_head(self):
        if self.Head is not None:
            temp = self.Head
            self.Head = temp.next
            temp.next = None
            return temp

    def delete_tail(self):
        if self.Head is not None:
            temp = self.Head
            if self.Head.next is None:
                self.Head = None
            else:
                while temp.next.next is not None:
                    temp = temp.next
                temp.next, temp = None, temp.next
            return temp

    def is_empty(self):
        return self.Head is None

    def reverse(self):
        """
        链表反转：
        1. 将旧链表最前面的节点作为当前节点保存在current对象
        2. 将current的next节点保存到next_node对象中
        3. 将current节点单独移出旧链表，作为新链表的最后一个节点，并将next指针指向prev
        4. 用prev对象保存current
        5. 将next_node作为current
        依次循环，直到current等于旧链表最后一个元素的next，即为None
        过程效果示例：
        旧：1 --> 2 --> 3 --> 4
        新：
        旧：2 --> 3 --> 4
        新：1
        旧：3 --> 4
        新：2 --> 1
        旧：4
        新：3 --> 2 --> 1
        旧：
        新：4 --> 3 --> 2 --> 1
        """
        prev = None
        current = self.Head

        while current:
            # Store the current node's next node.
            next_node = current.next
            # Make the current node's next point backwards
            current.next = prev
            # Make the previous node be the current node
            prev = current
            # Make the current node the next node (to progress iteration)
            current = next_node
        # Return prev in order to put the head at the end
        self.Head = prev


def main():
    A = LinkedList()
    print('Inserting 1st at Head')
    a1 = input()
    A.insert_head(a1)
    print('Inserting 2nd at Head')
    a2 = input()
    A.insert_head(a2)
    print('\nPrint List : ')
    A.print_list()
    print("\nInserting 1st at Tail")
    a3 = input()
    A.insert_tail(a3)
    print("Inserting 2nd at Tail")
    a4 = input()
    A.insert_tail(a4)
    print("\nPrint List : ")
    A.print_list()
    print("\nDelete Head")
    A.delete_head()
    print("Delete Tail")
    A.delete_tail()
    print("\nPrint List : ")
    A.print_list()
    print("\nReverse Linked List")
    A.reverse()
    print("\nPrint List : ")
    A.print_list()


if __name__ == '__main__':
    main()
